writeLines("************************************************************************************************************************")

# 	Filename: logdeaths_driver.R
# 	Description: Driver script to produce results based on regressions where logdeaths is the dependent variable.
writeLines("Description: Driver script to produce results based on regressions where logdeaths is the dependent variable.")
# 	Author: Barry Hashimoto
# 	Date: December 2019
# 	For: Barry Hashimoto, "Autocratic Consent to International Law: the Case of the International Criminal Court's Jurisdiction," International Organization.

timestamp()
set.seed(100)
options(scipen=999) # Prevent scientific formatting of numbers.

suppressPackageStartupMessages({library(Amelia); library(Zelig); library(brglm); library(MatchIt); library(cem); library(doBy); library(plyr); library(mitools); library(lattice); library(caret); library(memisc); library(arm); library(mitools); library(AICcmodavg); library(plm); library(mgcv); library(parallel); library(future)})

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part I: Analyze logdeaths for all states using the multiply imputed data sets.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("I.A. Analyze logdeaths for autocracies with the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only autocracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]} 
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 0,]} 
  # Run entire script with statistical analysis. Long-ish runtime!
  source("logdeathsMI_source.R", echo = F, spaced = T) 

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("I.B. Analyze logdeaths for democracies with the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only democracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 1,]}
  # Run entire script with statistical analysis.
  source("logdeathsMI_source.R", echo = F, spaced = T)

writeLines(""); writeLines("") 
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part II: Analyze logdeaths for only the capital-eligible states with the multiply imputed data sets.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("II.A. Analyze logdeaths for only the capital-eligible autocracies with the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only aid eligible autocracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}  
  mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
  aid.eligible.states <- mean.aid[mean.aid$capital.scaled>0, 1]
  for(z in 1:10){full$imputations[[z]] <- full$imputations[[z]][which(full$imputations[[z]]$ccode %in% aid.eligible.states),]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy==0,]}
  # Run entire script with statistical analysis.
  source("logdeathsMI_source.R", echo = F, spaced = T) 

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("II.B. Analyze logdeaths for only the capital-eligible democracies  with the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # loads imputed data in the object "full".
  # Keep only aid eligible democracies after 1998 Q3.
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$quarter>=154, ]}
  mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
  aid.eligible.states <- mean.aid[mean.aid$capital.scaled>0, 1]
  for(z in 1:10){full$imputations[[z]] <- full$imputations[[z]][which(full$imputations[[z]]$ccode %in% aid.eligible.states),]}
  for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][full$imputations[[z]]$democracy== 1,]}
  # Run entire script with statistical analysis. Long-ish runtime!
  source("logdeathsMI_source.R", echo = F, spaced = T) 

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part III: Analyze logdeaths using the unimputed data set, applying listwise deletion.")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("III.A. Analyze logdeaths for autocracies with the unimputed data set.")

  rm(list=ls())
  load("iccdata.RData")
  RegimeOption <- 0
  source("logdeathsLD_source.R", echo = F, spaced = T)   # Run entire script with statistical analysis.

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("III.B. Analyze logdeaths for democracies with the unimputed data set.")

  rm(list=ls())
  RegimeOption <- 1
  load("iccdata.RData")
  source("logdeathsLD_source.R", echo = F, spaced = T)   # Run entire script with statistical analysis.

writeLines("----------------------------------------------------------------------------------------------------------------------")

 writeLines("End of logdeaths_driver.R script")
 timestamp()
 writeLines(""); writeLines("")
writeLines("************************************************************************************************************************")
